<template>
  <div class="page-container">
    <!-- 顶部标题栏 -->
    <header class="add-header">
      <el-page-header content="新增月卡" @back="$router.back()" />
    </header>
    <main class="add-main">
      <!-- 表单容器1 -->
      <div class="form-container">
        <!-- 表单标题 -->
        <div class="title">车辆信息：</div>
        <!-- 车辆信息表单 -->
        <div class="form">
          <el-form ref="carInfoFormRef" :model="carInfoForm" :rules="carInfoRules" size="medium" label-width="100px">
            <el-form-item label="车主姓名：" prop="personName">
              <el-input v-model="carInfoForm.personName" placeholder="请输入车主姓名" />
            </el-form-item>
            <el-form-item label="联系方式：" prop="phoneNumber">
              <el-input v-model="carInfoForm.phoneNumber" placeholder="请输入联系方式" />
            </el-form-item>
            <el-form-item label="车辆号码：" prop="carNumber">
              <el-input v-model="carInfoForm.carNumber" placeholder="请输入车辆号码" />
            </el-form-item>
            <el-form-item label="车辆品牌：" prop="carBrand">
              <el-input v-model="carInfoForm.carBrand" placeholder="请输入车辆品牌" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!-- 表单容器2 -->
      <div class="form-container">
        <!-- 表单标题 -->
        <div class="title">最新一次月卡缴费信息：</div>
        <!-- 最新一次月卡信息表单 -->
        <div class="form">
          <el-form ref="feeFormRef" :model="feeForm" :rules="feeRules" size="medium" label-width="100px">
            <el-form-item label="有效日期：" prop="payTime">
              <el-date-picker
                v-model="feeForm.payTime"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额：" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount" placeholder="请输入支付金额" />
            </el-form-item>
            <el-form-item label="支付方式：" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod" placeholder="请选择支付方式">
                <el-option
                  v-for="item in paymentMethodList"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <!-- 底部按钮 -->
    <footer class="add-footer">
      <div class="btn-container">
        <el-button type="primary" @click="onAddCard">保存</el-button>
        <el-button class="reset-btn" @click="resetForm">重置</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCardAPI } from '@/api/car/carcard'
export default {
  name: 'AddCard',
  data() {
    return {
      // 车辆信息表单数据
      carInfoForm: {
        personName: '', // 车主姓名
        phoneNumber: '', // 车主电话
        carNumber: '', // 车牌号码
        carBrand: '' // 车辆品牌
      },
      // 车辆信息表单验证规则
      carInfoRules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/, message: '请输入正确的车牌号码', trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车牌品牌', trigger: 'blur' }
        ]
      },
      // 最近一次月卡缴费信息表单
      feeForm: {
        payTime: '', // 支付时间
        paymentAmount: '', // 支付金额
        paymentMethod: '' // 支付方式——支付宝:Alipay（默认）,微信:WeChat,线下:Cash
      },
      feeRules: {
        payTime: [
          { required: true, message: '请选择有效日期', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'blur' }
        ]
      },
      // 支付方式列表
      paymentMethodList: [
        {
          value: 'Alipay',
          label: '支付宝'
        },
        {
          value: 'WeChat',
          label: '微信'
        },
        {
          value: 'Cash',
          label: '线下'
        }
      ]
    }
  },
  methods: {
    // 新增月卡
    async onAddCard() {
      this.$refs.carInfoFormRef.validate(async valid => {
        if (valid) {
          this.$refs.feeFormRef.validate(async valid => {
            if (valid) {
              // 处理成接口要求的参数
              const payload = {
                ...this.carInfoForm,
                ...this.feeForm,
                cardStartDate: this.feeForm.payTime[0],
                cardEndDate: this.feeForm.payTime[1]
              }
              // 删除多余属性
              delete payload.payTime
              // 添加月卡
              await addCardAPI(payload)
              this.$message.success('新增月卡成功')
              this.$router.push('/car/card')
            }
          })
        }
      })
    },
    // 重置表单
    resetForm() {
      this.carInfoForm = {
        personName: '', // 车主姓名
        phoneNumber: '', // 车主电话
        carNumber: '', // 车牌号码
        carBrand: '' // 车辆品牌
      }
      this.feeForm = {
        payTime: '', // 支付时间
        paymentAmount: '', // 支付金额
        paymentMethod: '' // 支付方式——支付宝:Alipay（默认）,微信:WeChat,线下:Cash
      }
    }
  }
}
</script>

<style scoped lang="scss">
.page-container {
  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    margin-bottom: 30px;
    height: 64px;
    background-color: #fff;
    border-bottom: 1px solid #eee;

    ::v-deep .el-page-header {
      .el-page-header__title {
        font-size: 16px;
        font-weight: 600;
        color: #444;
      }

      .el-page-header__content {
        font-size: 16px;
        font-weight: 600;
        color: #444;
      }
    }
  }

  .add-main {
    padding-left: 50px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
        font-weight: 600;
        color: #444;
      }

      .form {
        padding: 20px 65px 24px;

        .el-input, .el-select, .el-range-editor {
          width: 400px;
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    width: 100%;
    padding: 20px 0 0 270px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;

    .reset-btn {
      margin-left: 30px;
    }
  }
}
</style>
